package server.life;

import database.BeePool;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import tools.FileoutputUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class MapleMonsterInformationProvider {
    private static MapleMonsterInformationProvider instance;
    private Map<Integer, String> mobCache;
    private Map<Integer, ArrayList<MonsterDropEntry>> drops;
    private List<MonsterGlobalDropEntry> globaldrops;

    protected MapleMonsterInformationProvider() {
        this.mobCache = (Map<Integer, String>) new HashMap();
        this.drops = (Map<Integer, ArrayList<MonsterDropEntry>>) new ConcurrentHashMap();
        this.globaldrops = (List<MonsterGlobalDropEntry>) new ArrayList();
        this.retrieveGlobal();
    }

    public static MapleMonsterInformationProvider getInstance() {
        return MapleMonsterInformationProvider.instance;
    }

    public List<MonsterGlobalDropEntry> getGlobalDrop() {
        return this.globaldrops;
    }

    public Map<Integer, String> getAllMonsters() {
        if (this.mobCache.isEmpty()) {
            MapleDataProvider stringData = MapleDataProviderFactory.getDataProvider("String.wz");
            MapleData mobsData = stringData.getData("Mob.img");
            for (MapleData itemFolder : mobsData.getChildren()) {
                this.mobCache.put(Integer.valueOf(Integer.parseInt(itemFolder.getName())), MapleDataTool.getString("name", itemFolder, "NO-NAME"));
            }
        }
        return this.mobCache;
    }

    private void retrieveGlobal() {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try (Connection con = BeePool.getConnection()) {
            ps = con.prepareStatement("SELECT * FROM drop_data_global WHERE chance > 0");
            rs = ps.executeQuery();
            while (rs.next()) {
                this.globaldrops.add(new MonsterGlobalDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("continent"), rs.getByte("dropType"), rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getShort("questid")));
            }
            rs.close();
            ps.close();
            
        } catch (SQLException e) {
            FileoutputUtil.outError("logs/资料库异常.txt", e);
        }
    }

    public ArrayList<MonsterDropEntry> retrieveDrop(int monsterId) {
        if (this.drops.containsKey(Integer.valueOf(monsterId))) {
            return this.drops.get(Integer.valueOf(monsterId));
        }
        ArrayList<MonsterDropEntry> ret = (ArrayList<MonsterDropEntry>) new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try (Connection con = BeePool.getConnection()) {
            ps = con.prepareStatement("SELECT * FROM drop_data WHERE dropperid = ?");
            ps.setInt(1, monsterId);
            rs = ps.executeQuery();
            while (rs.next()) {
                ret.add(new MonsterDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getShort("questid")));
            }
            rs.close();
            ps.close();
            
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    
                }
            } catch (SQLException ignore) {
                FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) ignore);
                return ret;
            }
        } catch (SQLException e) {
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
            return ret;
        }
        finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ignore2) {
                FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) ignore2);
                return ret;
            }
        }
        this.drops.put(Integer.valueOf(monsterId), ret);
        return ret;
    }

    public void clearDrops() {
        this.drops.clear();
        this.globaldrops.clear();
        this.retrieveGlobal();
    }

    public int getDropQuest(int monsterId) {
        int quest = 0;
        try (Connection con = BeePool.getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT questid FROM drop_data where dropperid = ?");
            ps.setInt(1, monsterId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                quest = rs.getInt("questid");
            }
            rs.close();
            ps.close();
            
        } catch (SQLException e) {
            System.out.println("Error getDropQuest" + e);
            FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) e);
        }
        return quest;
    }

    public List<Integer> getMobByItem(int itemId) {
        List<Integer> mobs = (List<Integer>) new LinkedList();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            try (Connection con = BeePool.getConnection()) {
                ps = con.prepareStatement("SELECT * FROM drop_data WHERE itemid = ?");
                ps.setInt(1, itemId);
                rs = ps.executeQuery();
                while (rs.next()) {
                    int mobid = rs.getInt("dropperid");
                    if (!mobs.contains(Integer.valueOf(mobid))) {
                        mobs.add(Integer.valueOf(mobid));
                    }
                }
                rs.close();
                ps.close();
                
            }
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ignore) {
                System.out.println("Error getMobByItem" + ignore);
                FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) ignore);
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Error getMobByItem" + e);
            FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) e);
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ignore) {
                System.out.println("Error getMobByItem" + ignore);
                FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) ignore);
                return null;
            }
        }
        finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ignore2) {
                System.out.println("Error getMobByItem" + ignore2);
                FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) ignore2);
                return null;
            }
        }
        return mobs;
    }

    public int getDropChance(int monsterId) {
        int chance = 0;
        try (Connection con = BeePool.getConnection()) {
            PreparedStatement ps = con.prepareStatement("SELECT chance FROM drop_data where dropperid = ?");
            ps.setInt(1, monsterId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                chance = rs.getInt("chance");
            }
            rs.close();
            ps.close();
            
        } catch (SQLException e) {
            System.out.println("Error getDropChance" + e);
            FileoutputUtil.outputFileError("logs/资料库异常.txt", (Throwable) e);
        }
        return chance;
    }

    public void UpdateDropChance(int chance, int dropperid, int itemid) {
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement("UPDATE drop_data SET chance = ? WHERE dropperid = ? AND itemid = ?")) {
            ps.setInt(1, chance);
            ps.setInt(2, dropperid);
            ps.setInt(3, itemid);
            ps.executeUpdate();
            ps.close();
            
        } catch (SQLException ex) {
            System.out.println("Error UpdateDropChance" + ex);
            FileoutputUtil.outputFileError("logs/更新爆率几率异常.txt", (Throwable) ex);
        }
    }

    public void AddDropData(int chance, int dropperid, int itemid, int questid) {
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement("INSERT INTO drop_data SET chance = ? , dropperid = ? , itemid = ?, questid = ?")) {
            ps.setInt(1, chance);
            ps.setInt(2, dropperid);
            ps.setInt(3, itemid);
            ps.setInt(4, questid);
            ps.executeUpdate();
            ps.close();
            
        } catch (SQLException ex) {
            System.out.println("Error AddDropData" + ex);
            FileoutputUtil.outputFileError("logs/添加爆率异常.txt", (Throwable) ex);
        }
    }

    public void DeleteDropData(int dropperid, int itemid) {
        try (Connection con = BeePool.getConnection();
             PreparedStatement ps = con.prepareStatement("DELETE FROM drop_data WHERE dropperid = ? AND itemid = ?")) {
            ps.setInt(1, dropperid);
            ps.setInt(2, itemid);
            ps.executeUpdate();
            ps.close();
            
        } catch (SQLException ex) {
            System.out.println("Error DeleteDropData" + ex);
            FileoutputUtil.outputFileError("logs/删除爆率异常.txt", (Throwable) ex);
        }
    }

    static {
        instance = new MapleMonsterInformationProvider();
    }
}
